//
//  DDVipAlertView.m
//  LuPingBox
//
//  Created by 茶 on 2023/11/1.
//  Copyright © 2023 DD. All rights reserved.
//

#import "CAVipAlertView.h"

#define UIColorWhite UIColor.whiteColor

@interface CAVipAlertView ()

@property (nonatomic, strong) UIButton *checkBtn;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, copy) void(^closeBlock)(void);

@end

@implementation CAVipAlertView

+ (instancetype)showWithTitle:(NSString *)title completionBlock:(void (^)(void))completionBlock {
    CGFloat width = CARatioWidth(280);
    CAVipAlertView *view = [[CAVipAlertView alloc] initWithFrame:CGRectMake(0, 0, width, CARatioWidth(270))];
    view.closeBlock = completionBlock;
    view.title = title;
    [LEEAlert alert].config
    .LeeCustomView(view)
    .LeeMaxWidth(width)
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderColor(UIColor.clearColor)
    .LeeShow();
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface{
    UIView *backView = [UIView new];
    backView.backgroundColor = UIColor.whiteColor;
    backView.layer.cornerRadius = CARatioWidth(20);
    backView.layer.borderColor = UIColorWhite.CGColor;
    backView.layer.borderWidth = 1;
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.top.ca_offset(20);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera_vip_top"]];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
    }];
    
    UILabel *contentLabel = [UILabel new];
    contentLabel.text = @"继续使用该功能需要VIP";
    contentLabel.font = CAFontScaleRegular(21);
    contentLabel.textColor = UIColor.blackColor;
    [backView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ca_offset(49);
        make.centerX.mas_equalTo(0);
    }];
    _titleLabel = contentLabel;
    
    UILabel *tipLabel = [UILabel new];
    tipLabel.text = @"订阅￥15.00/年的完整版\n无限制访问";
    tipLabel.numberOfLines = 2;
    tipLabel.font = CAFontScaleRegular(13);
    tipLabel.textColor = HexColor(@"#666666");
    tipLabel.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ca_offset(86);
        make.centerX.mas_equalTo(0);
    }];
    
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkBtn setImage:[UIImage imageNamed:@"Vip_Uncheck"] forState:UIControlStateNormal];
    [checkBtn setImage:[UIImage imageNamed:@"Vip_Check"] forState:UIControlStateSelected];
    [checkBtn addTarget:self action:@selector(checkEvent:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:checkBtn];
    [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ca_offset(144);
        make.left.ca_offset(46);
    }];
    _checkBtn = checkBtn;
    
    UILabel *label = [UILabel new];
    label.text = @"我已阅读";
    label.font = CAFontScaleRegular(12);
    label.textColor = HexColor(@"#666666");
    [backView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(checkBtn);
        make.left.mas_equalTo(checkBtn.mas_right).ca_offset(5);
    }];
    
    UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [userBtn.titleLabel setFont:CAFontScaleRegular(12)];
    [userBtn setTitle:@"《用户协议》" forState:UIControlStateNormal];
    [userBtn addTarget:self action:@selector(userEvent) forControlEvents:UIControlEventTouchUpInside];
    [userBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [backView addSubview:userBtn];
    [userBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(checkBtn);
        make.left.mas_equalTo(label.mas_right);
    }];
    
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [privacyBtn.titleLabel setFont:CAFontScaleRegular(12)];
    [privacyBtn setTitle:@"《隐私政策》" forState:UIControlStateNormal];
    [privacyBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [privacyBtn addTarget:self action:@selector(privacyEvent) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:privacyBtn];
    [privacyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(checkBtn);
        make.left.mas_equalTo(userBtn.mas_right);
    }];
    
    UIButton *unlockBtn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.cornerRadius = CARatioWidth(15.5);
        button.clipsToBounds = YES;
        [button.titleLabel setFont:CAFontScaleRegular(14)];
        [button setBackgroundColor:HexColor(@"#29282A")];
        [button setTitle:@"订阅" forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(unlockEvent) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.ca_offset(31);
            make.bottom.ca_offset(-27);
        }];
        button;
    });

    UIButton *cancelBtn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.cornerRadius = CARatioWidth(15.5);
        button.clipsToBounds = YES;
        button.layer.borderColor = HexColor(@"#433024").CGColor;
        button.layer.borderWidth = 1;
        [button.titleLabel setFont:CAFontScaleRegular(14)];
        [button setBackgroundColor:UIColor.whiteColor];
        [button setTitle:@"取消" forState:UIControlStateNormal];
        [button setTitleColor:HexColor(@"#29282A") forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closeEvent) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.ca_offset(31);
            make.bottom.ca_offset(-27);
        }];
        button;
    });
   
    [@[cancelBtn, unlockBtn] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:CARatioWidth(15) leadSpacing:CARatioWidth(18) tailSpacing:CARatioWidth(18)];
}

- (void)userEvent {
    NSURL *url = [NSURL URLWithString:kAgreement];
    if ([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

- (void)privacyEvent {
    NSURL *url = [NSURL URLWithString:kPrivacy];
    if ([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}

- (void)checkEvent:(UIButton *)button {
    button.selected = !button.isSelected;
}


- (void)unlockEvent {
    if (!_checkBtn.isSelected) {
        [kKeyWindow makeToast:@"未同意协议，请同意后继续"];
    } else {
        [LEEAlert closeWithCompletionBlock:self.closeBlock];
    }
}

- (void)closeEvent {
    [LEEAlert closeWithCompletionBlock:nil];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    if (title && title.length > 0) {
        _titleLabel.text = title;
    }
}
@end
